package com.zxl.grocery.webmagic.pipeline;


import com.zxl.grocery.webmagic.common.persistence.model.CrawlerBeautiful;
import com.zxl.grocery.webmagic.service.ICrawlerBeautifulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * info:美图爬虫
 * Created by Lzx on 2017/7/12.
 */
@Repository
public class BeautifulPipeline implements Pipeline {

    @Autowired
    private ICrawlerBeautifulService iCrawlerBeautifulService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            if (entry.getKey().equals("beautiful")) {
                CrawlerBeautiful beautiful=(CrawlerBeautiful) entry.getValue();
                HashMap<String, Object> conditionMap = new HashMap<String, Object>();
                conditionMap.put("link",beautiful.getLink());
                List<CrawlerBeautiful> beautifuls = (List<CrawlerBeautiful>)iCrawlerBeautifulService.selectByMap(conditionMap);

                if (beautifuls.size() < 1) {//检查链接是否已存在
                    beautiful.setTypeId(1);
                    beautiful.setAuthor("zxl");
                    beautiful.setCreateDate(new Date());
                    beautiful.setStatus(1);
                    beautiful.setPlacedTop(0);
                    beautiful.setRecommend(0);
                    beautiful.setModifyDate(new Date());
                    iCrawlerBeautifulService.insert(beautiful);
                }
            }

        }
    }
}
